﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client.XML;
using Client.Common;
using Client.UserControls;
using Common;

namespace Client
{
    public partial class FrmMain : Form
    {
        private List<WorkPlanItem> lstPlanItem = null;
        public FrmMain()
        {
            InitializeComponent();
        }
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            // 输入项有错误
            if (!this.workPlan1.IsValidateAndReOrder())
            {
                return;
            }

            PlanList pl = Helper.GetPlanList();
            DayPlan mp =null;

            // 如果今天还没有计划
            if (!Helper.HasPlanedToday(pl, out mp))
            {
                mp = new DayPlan();
                mp.Date = DateTime.Now.ToString("yyyy-MM-dd");
                mp.ID = Guid.NewGuid().ToString();
                mp.PlanItems.AddRange(this.workPlan1.GetPlanItems());
                pl.Plans.Add(mp);
            }
            // 修改当日计划
            else
            {
                mp.PlanItems.Clear();
                mp.PlanItems.AddRange(this.workPlan1.GetPlanItems());
            }

            XmlOperator.Serialize(CommonData.XmlFilePath, pl);

            this.btnCancel.Text = "关闭";
            // 清空PlanList，让下次请求重新获取
            Helper.ClearCacheForPlanList();
            MessageBox.Show("保存成功!");
        }

        

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            // 初始化注册表值
            RegEditor.InitReg();

            DayPlan plan = null;
            if (Helper.HasPlanedToday(Helper.GetPlanList(), out plan))
            {
                this.workPlan1.FillPlan(plan);
            }
        }
    }
}
